package lowijs.XfDroid;

import static lowijs.XfDroid.FriendDBAdapter.*;
import static lowijs.XfDroid.XfDroidConstants.*;

import com.googlecode.xfdroid.R;
import java.text.SimpleDateFormat;
import java.util.List;

import lowijs.XfireClient.Screenshot;
import lowijs.XfireClient.XfireObserver;
import lowijs.XfireClient.event.EventManager;
import lowijs.XfireClient.event.FriendEvent;
import lowijs.XfireClient.event.ScreenshotsEvent;
import lowijs.XfireClient.event.XfireEvent;
import lowijs.XfireClient.util.ScreenshotComparator;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Screenshots extends ListActivity implements XfireObserver {
	private ScreenshotsAdapter adapter = null;
	
	private ProgressDialog pd = null;

	private FriendDBAdapter db;
	
	public void onCreate(Bundle b) {
		super.onCreate(b);

		setContentView(R.layout.screenshots);
		long rowid = getIntent().getLongExtra(KEY_ROWID, 0);

		db = FriendDBAdapter.getInstance(getBaseContext());
		String nickname = db.nickname(rowid);

		String title = getString(R.string.screenshots_title);
		setTitle(title.replace(ARG_0, nickname));
	}
	
	public void onStart() {
		super.onStart();
		EventManager.addObserver(this);

		if (adapter == null) {
			showProgressDialog();
		}

		long rowid = getIntent().getLongExtra(KEY_ROWID, 0);

		FriendEvent rie = new FriendEvent(XfireEvent.XF_REQINFO, new long[] {rowid});
		EventManager.fireEvent(rie);
	}
	
	public void onSaveInstanceState(Bundle b) {
		b.putBoolean(STATE_PROGRESS_DIALOG, pd != null && pd.isShowing());
		super.onSaveInstanceState(b);
	}
	
	public void onStop() {
		super.onStop();
		EventManager.removeObserver(this);		
		
		if (pd != null) {
			pd.dismiss();
		}
	}
	
	public void onRestoreInstanceState(Bundle b) {
		if (b.getBoolean(STATE_PROGRESS_DIALOG)) {
			showProgressDialog();
		}

		b.remove(STATE_PROGRESS_DIALOG);

		super.onRestoreInstanceState(b);
	}
	
	public void onListItemClick(ListView l, View v, int position, long id) {
		Screenshot ss = adapter.getItem(position);
		
		Intent i = new Intent(this, ViewScreenshot.class);
		i.putExtra(GUID, ss.getGuid());
		startActivity(i);
		
		super.onListItemClick(l, v, position, id);
	}
	
	private void showProgressDialog() {
		pd = new ProgressDialog(this);
		pd.setTitle(R.string.please_wait);
		pd.setMessage(getString(R.string.getting_screenshots));
		pd.setCancelable(false);
		pd.show();
	}
	
	public void handleEvent(XfireEvent event) {
		switch(event.getEventType()) {
		case XfireEvent.XF_SCREENSHOTS:
			ScreenshotsEvent se = (ScreenshotsEvent)event;
			pd.dismiss();

			if (se.getScreenshots() == null) {
				return;
			}

			adapter = new ScreenshotsAdapter(this, R.layout.screenshotrow, se.getScreenshots());
			screenshotsHandler.sendEmptyMessage(0);

			return;
		}
	}
	
	private Handler screenshotsHandler = new Handler() {
		public void handleMessage(Message m) {
			adapter.sort(new ScreenshotComparator());
			setListAdapter(adapter);
		}
	};
	
	private class ScreenshotsAdapter extends ArrayAdapter<Screenshot> {
		public ScreenshotsAdapter(Context ctx, int resId, List<Screenshot> list) {
			super(ctx, resId, list);
		}

		public View getView(int pos, View v, ViewGroup parent) {
			if (v == null) {
				LayoutInflater infl = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
				v = infl.inflate(R.layout.screenshotrow, null);
			}

			Screenshot ss = getItem(pos);

			TextView tvDescription = (TextView)v.findViewById(R.id.tvDescription);
			TextView tvGamename = (TextView)v.findViewById(R.id.tvGamename);
			TextView tvTime = (TextView)v.findViewById(R.id.tvTime);

			tvDescription.setText(ss.getDescription());
			int gid = ss.getGameid();
			tvGamename.setText(db.gamename(gid));

			SimpleDateFormat sdf = new SimpleDateFormat(getString(R.string.datetime_format));
			tvTime.setText(sdf.format(ss.getDate()));

			return v;
		}
	}
}
